package com.java.mybatis.controller;

import com.java.mybatis.entity.*;
import com.java.mybatis.service.EmployeesService;
import com.java.mybatis.service.MenusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * (Employees)表控制层
 * @author makejava
 * @since 2021-01-15 17:10:28
 */
@Controller
@RequestMapping("/employees/")
public class EmployeesController {
    /**
     * 服务对象
     */
    @Autowired
    private EmployeesService employeesService;

    @Autowired
    private MenusService menusService;

    @RequestMapping("employeeslike")
    public String employeeslike(Model model,String employeesName) throws Exception {
        System.out.println("测试数据");
        System.out.println(employeesName);
        List<Employees> employeeslike = this.employeesService.employeeslike("%"+employeesName+"%");
        if (employeeslike.size()>0){
            System.out.println(employeeslike);
            //返回给物流员管理页面
            model.addAttribute("ems",employeeslike);
            return "System/role-edit";
        }
        return "集合没有数据";
    }
    /**
     * 牟恩跃--条件查询
     * @param employees
     * @return
     */
    @RequestMapping("employeesList")
    public String employeesList(Employees employees,Model model){
        //获取数据存入集合
        List<Employees> employees1 = this.employeesService.employeesList(employees);
        //获取集合的个数
        int size = employees1.size();
        model.addAttribute("size",size);
        //返回给物流员管理页面
        model.addAttribute("ems",employees1);
        return "System/role-edit";
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Employees selectOne(Integer id) {
        return this.employeesService.queryById(id);
    }

    @RequestMapping("EmpLogin")
    public String EmpLogin(HttpServletRequest request,String empUserName,String empUserPwd){
        //根据登录时填写的信息查找对应的员工
        Employees employees2 = employeesService.login(empUserName,empUserPwd);
        System.out.println("---------------------"+employees2);
        System.out.println("---------------------"+empUserName+"---"+empUserPwd);
        //不等于空时   则代表信息正确  跳转到首页
        if (employees2!=null) {
            //employees1.getRolesId()   获取当前登录人的权限  根据权限查询对应的菜单
           List<Menus> menus = menusService.queryMenus(employees2.getRoles().getRolesId());

           //将登录人信息和对应菜单信息存入session
           request.getSession().setAttribute("emp",employees2);
           request.getSession().setAttribute("empMenus",menus);
            return "redirect:/System/index.jsp";
        }else{
            return "System/login";
        }
    }
    @GetMapping("queryAllEmp")
    public String queryAllEmp(Model model){
        //调用查询所有员工信息的方法
        List<Employees> employees = employeesService.queryAllEmp();
        //将查询到的信息存入model中
        model.addAttribute("emp",employees);
        //得到总条数的方法
        model.addAttribute("ids",employees.size());
        //返回对应的页面
        return "System/admin-list";
    }
    /**
     * 分页查询所有员工信息的方法
     * @return 对象序列
     */
    @RequestMapping("getAllEmp")
    public String getAllEmp(Model model, Integer page, HttpSession session){
        try {
            List<Employees> allEmp = employeesService.getAllEmp((page-1)*7,7);
            for (Employees emp1 : allEmp) {
                System.out.println(emp1);
            }
            model.addAttribute("emp1",allEmp);
            //存入当前页码
            session.setAttribute("thisEmpPage",page);
            //获取总行数
            Integer empCount = employeesService.getEmpCount();
            System.out.println("查到的总行数:"+empCount);
            //将获取到的总行数存入model中
            model.addAttribute("count",empCount);
            //获取总共有多少页
            int empPageMax= empCount % 7 ==0 ? empCount / 7 : empCount / 7 + 1;
            model.addAttribute("empPageText","当前第"+page+"页,共"+empPageMax+"页");
            //存总共多少页
            model.addAttribute("empPageMax",empPageMax);
        } catch (Exception e) {
            e.printStackTrace();//打印堆栈信息
        }
        return "System/admin-list";
    }
    /**
     * 删除单个员工的方法
     * */
    @RequestMapping("delEmpById")
    public String delEmpById(Integer id){
        System.out.println("获取到的员工编号:"+id);
        try {
            //调用禁用的方法执行软删除
            employeesService.upEmployeesCodeDown(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //返回对应的页面
        return "redirect:/employees/getAllEmp";
    }
    /**
     * 批量删除员工的方法
     * @return
     */
    @RequestMapping("upEmployeesAll")
    public String upEmployeesAll(String ids1) throws Exception {
        String[] split = ids1.split(",");
        System.out.println("获取到的所有员工编号:"+ids1);
        for (String s : split) {
            System.out.println("获取到的所有公告编号:"+s);
            employeesService.upEmployeesCodeDown(Integer.parseInt(s));//调用禁用的方法
        }
        return "redirect:/employees/getAllEmp";//返回对应的页面
    }
    /**
     * 新增员工信息的方法
     * @return 添加成功返回大于0的整数，修改失败返回0
     */
    @RequestMapping("insertEmp")
    public String insertEmp(HttpServletRequest request,Model model,String employeesName, String employeesAge,String employeesAddress,String employeesUsername,String employeesPhone,String employeesEmil,Integer role,Integer department){
        System.out.println("前台传过来的员工名字:"+employeesName);
        //创建Emplyees类的具体实例
        Employees employees=new Employees();
        employees.setEmployeesName(employeesName);//员工姓名
        employees.setEmployeesAge(employeesAge);//员工年龄
        employees.setEmployeesPhone(employeesPhone);//员工电话
        employees.setEmployeesUsername(employeesUsername);//员工账号
        employees.setEmployeesPwd("123456");//默认一个密码
        employees.setEmployeesEmil(employeesEmil);//员工邮箱
        employees.setEmployeesAddress(employeesAddress);//员工住址
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//日期格式化
        employees.setEmployeesTime(simpleDateFormat.format(new Date()));//员工入职时间
        Roles roles1=new Roles();//创建角色表的具体实例
        roles1.setRolesId(role);//添加对应的角色
        employees.setRoles(roles1);//员工职位(关联职位表id)
        Department department1=new Department();//创建Department类的对象
        department1.setDepartmentId(department);
        employees.setDepartment(department1);//员工所属的部门
        employees.setEmployeesCode(1);//软删除
        employees.setEmployeesMark("备用字段");
        //调用添加部门信息的方法
        Employees addEmp = employeesService.insert(employees);//添加部门信息
        //将返回的数据存入model中
        model.addAttribute("emp1",addEmp);
        //返回对应的页面
        return "redirect:/employees/getAllEmp";
    }
    /**
     * 修改员工信息的方法
     * @return 修改成功返回大于0的整数，修改失败返回0
     */
    @RequestMapping("updateEmp")
    public String updateEmp(HttpSession session, Model model,Integer employeesId,String employeesAge,String employeesAddress, String employeesPhone,Integer roles,Integer department){
        //测试
        System.out.println("获取到前台传过来的员工年龄:"+roles);
        //创建Emplyees类的具体实例
        Employees employees=new Employees();
        employees.setEmployeesId(employeesId);
        employees.setEmployeesAge(employeesAge);//员工年龄
        employees.setEmployeesPhone(employeesPhone);//员工电话
        employees.setEmployeesAddress(employeesAddress);//员工住址
        Roles roles2=new Roles();//创建角色表的具体实例
        roles2.setRolesId(roles);//修改对应的角色
        employees.setRoles(roles2);//员工职位(关联职位表id)
        Department department1=new Department();//创建Department类的对象
        department1.setDepartmentId(department);
        employees.setDepartment(department1);//员工所属的部门
        employees.setEmployeesCode(1);//软删除
        employees.setEmployeesMark("备用字段");
        System.out.println("-----------------------------"+employees);
        //employees.setEmployeesId((Integer) session.getAttribute("emp1"));
        //调用修改部分信息的方法
        int update = employeesService.update(employees);
        System.out.println("-------------=========" + update);
        //返回对应的页面
        return "redirect:/employees/getAllEmp";
    }

    /**
     * 查询单个数据的方法
     * @param session session对象
     * @param id 员工编号
     */
    @RequestMapping("getEmployeesOne")
    public void getEmployeesOne(HttpSession session,Integer id){
        Employees employees = employeesService.queryById(id);
        System.out.println("---------------------" + id);
        session.setAttribute("employeesOne",employees);
    }
    /**
     * 根据员工姓名来查询员工信息
     * @param employeesName 员工姓名
     * @return 对象序列
     * */
    @PostMapping("selectByLikeName")
    public String selectByLikeName(Model model,String employeesName){
        System.out.println("-----------------------------------------" + employeesName);
        if (employeesName!="" && employeesName!=null) {
            //调用查询部门编号或者部门名称的方法
            List<Employees> employees = employeesService.queryLikeName(employeesName);
            //查返回的值存入model中
            model.addAttribute("emp1",employees);
            model.addAttribute("count",employees.size());//得到总条数的方法
            model.addAttribute("empPageText","当前第"+1+"页,共"+1+"页");
            //返回对应的页面
            return "System/admin-list";
        }else {
            return "redirect:/employees/getAllEmp?page=1";
        }
    }

    /**
     * 分页查询所有员工信息
     * @param page 当前页码
     * @param model
     * @param session 单次会话
     * @return
     */
    @RequestMapping("SelEmpAll")
    public String SelEmpAll(Integer page,Model model,HttpSession session){
        try {
            session.removeAttribute("EmpNameLike");
            //从几条开始查询，每页几条数据
            List<Employees> employees = employeesService.SelEmpAll((page - 1) * 6, 6);
            model.addAttribute("AllEmployees",employees);
            //存入当前页码
            model.addAttribute("thisPage",page);
            //查询总行数
            Integer count = employeesService.SelEmpCount();
            model.addAttribute("EmpCount",count);
            //总共有多少页
            int EmpMax = count % 6 == 0 ? count / 6 : count / 6 + 1;
            model.addAttribute("pageText","当前第"+page+"页,共"+EmpMax+"页");
            //存入总共有多少页
            model.addAttribute("EmpMax",EmpMax);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "System/role-edit";
    }

    @RequestMapping("SelEmpNameLike")
    public String SelEmpNameLike(String name,Integer page,Model model,HttpSession session){
        try {
            if(name!=null&&name!="") {
                List<Employees> employees = employeesService.SelEmpNameLike(name, (page - 1) * 3, 3);
                model.addAttribute("AllEmployees", employees);
                Integer count = employeesService.SelEmpNameLikeCount(name);
                //存入当前页码
                model.addAttribute("thisPage", page);
                model.addAttribute("EmpCount", count);
                //总共有多少页
                int EmpMax = count % 3 == 0 ? count / 3 : count / 3 + 1;
                model.addAttribute("pageText", "当前第" + page + "页,共" + EmpMax + "页");
                //存入总共有多少页
                model.addAttribute("EmpMax", EmpMax);
                session.setAttribute("EmpNameLike", name);
            }else {
                return "redirect:/employees/SelEmpAll?page=1";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "System/role-edit";
    }
}